package com.chenss.dragonser.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.chenss.dragonser.R;
import com.chenss.dragonser.adapter.GoodsListAdapter;
import com.chenss.dragonser.adapter.RecordListAdapter;
import com.chenss.dragonser.app.AppInfo;
import com.chenss.dragonser.app.Constant;
import com.chenss.dragonser.base.BaseActivity;
import com.chenss.dragonser.model.RecordListModel;
import com.chenss.dragonser.network.NetworkRequest;
import com.chenss.dragonser.network.RequestCallbcak;
import com.chenss.dragonser.response.RecordListResponse;
import com.chenss.dragonser.utils.GsonUtil;
import com.chenss.dragonser.utils.ToastUtil;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;

import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

/**
 * PageName: 收支记录
 * Create by chenss at 2018/4/20 2:54
 */
public class MoneyRecordActivity extends BaseActivity implements RequestCallbcak,BaseRefreshListener {
    LinearLayout ll_left;//左边按钮
    TextView tv_page_title;
    private TextView tv_tab_left;
    private TextView tv_tab_right;
    ListView mListView;
    List<RecordListModel> list = new ArrayList<>();
    RecordListAdapter listAdapter;

    public NetworkRequest networkRequest;//网络请求
    public ArrayMap<String, String> parmMap;//请求参数封装

    String type="1";//1.收入  2.支出

    PullToRefreshLayout pullToRefreshLayout;

    private int pageNo = 1;//当前页数
    private int totalpage = 0;//总条数
    private final static int REFRESHING = 0x010;
    private final static int LOADMORE = 0x020;
    int freshState = REFRESHING;//默认是刷新

    @Override
    public void initParms(Bundle parms) {

    }

    @Override
    public int bindLayout() {
        return R.layout.activity_money_record;
    }

    @Override
    public void initView(View view) {
        ll_left=findView(R.id.ll_left);//左边按钮
        tv_page_title=findView(R.id.tv_page_title);
        ll_left.setVisibility(View.VISIBLE);
        ll_left.setOnClickListener(this);
        tv_page_title.setText("收支记录");

        tv_tab_left = findView(R.id.tv_tab_left);
        tv_tab_right = findView(R.id.tv_tab_right);
        tv_tab_left.setOnClickListener(this);
        tv_tab_right.setOnClickListener(this);
        tv_tab_left.setText("收入");
        tv_tab_right.setText("支出");


        pullToRefreshLayout = findView(R.id.pullToRefreshLayout);
        pullToRefreshLayout.setRefreshListener(this);
        mListView = findView(R.id.mListView);

        listAdapter = new RecordListAdapter(this, list);
        mListView.setAdapter(listAdapter);

    }

    @Override
    public void doBusiness(Context mContext) {
        networkRequest = new NetworkRequest(mContext, this);
        tv_tab_left.performClick();
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()){
            case R.id.ll_left:
                finish();
                break;
            case R.id.tv_tab_left:
                selected();
                tv_tab_left.setSelected(true);
                type="1";
                freshState=REFRESHING;
            getStoreLog(true);
                break;
            case R.id.tv_tab_right:
                selected();
                tv_tab_right.setSelected(true);
                type="2";
                freshState=REFRESHING;
                getStoreLog(true);
                break;
        }
    }
    //重置所有文本的选中状态
    public void selected(){
        tv_tab_left.setSelected(false);
        tv_tab_right.setSelected(false);
    }

    /**
     * 获取收支明细
     */
    private void getStoreLog(boolean isShow) {
        parmMap = new ArrayMap<>();
        Constant.setCommonParams(parmMap, Constant.App.Wallet, Constant.classes.StoreLog);
        parmMap.put("store_id", AppInfo.getUser(this).getStore_id());//服务站用户id
        parmMap.put("type",type);//1.收入  2.支出
        parmMap.put("page", pageNo+"");//第几页
        networkRequest.postJsonRequest(Constant.serverPath, parmMap, Constant.Actionid.NETWORK_REQUEST_CODE_1, isShow, Constant.classes.StoreLog);
    }
    @Override
    public void refresh() {
        freshState = REFRESHING;
        pageNo = 1;
        getStoreLog(false);
    }

    @Override
    public void loadMore() {

            freshState = LOADMORE;
            pageNo++;
            getStoreLog(false);
    }
    @Override
    public void onFaild(int orderCode, String result) {
        ToastUtil.showShort(this, result);
        pullToRefreshLayout.finishLoadMore();
        pullToRefreshLayout.finishRefresh();
    }

    @Override
    public void onSucceed(int orderCode, String result) throws JSONException {
        Logger.getLogger(result);
        if (orderCode == Constant.Actionid.NETWORK_REQUEST_CODE_1) {
            pullToRefreshLayout.finishLoadMore();
            pullToRefreshLayout.finishRefresh();
            RecordListResponse recordListResponse = GsonUtil.GsonToBean(result,RecordListResponse.class);
            if (null != recordListResponse) {
                if (0 == recordListResponse.getStatus()) {
                    if(freshState==REFRESHING){
                        list.clear();
                    }
                    List<RecordListModel> models=recordListResponse.getData();
                    if(null!=models&&models.size()>0){
                        list.addAll(models);
                        listAdapter.setDataList(list);
                    }else{
                        if(freshState==REFRESHING){
                            listAdapter.setDataList(list);
                            ToastUtil.showShort(this,"暂无数据");
                        }else{
                            ToastUtil.showShort(this,"没有更多数据了");
                        }

                    }

                }
            }


        }
    }

}
